package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Message;
import cn.wolfcode.trip.base.domain.MessageInfo;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.MessageInfoMapper;
import cn.wolfcode.trip.base.mapper.MessageMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.MessageInfoQueryObject;
import cn.wolfcode.trip.base.service.IMessageInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MessageInfoServiceImpl implements IMessageInfoService {

    @Autowired
    private MessageInfoMapper messageInfoMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MessageMapper messageMapper;

    @Override
    public PageInfo query(MessageInfoQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<MessageInfo> list = messageInfoMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void save(MessageInfo messageInfo) {
        messageInfoMapper.insert(messageInfo);
        List<User> users = userMapper.selectAll();
        for (User user : users) {
            Message message = new Message();
            message.setMsgInfo(messageInfo);
            message.setReceiver(user);
            messageMapper.insert(message);
        }
    }
}
